//
//  YYELessonAnswerViewController.m
//  YuYingEDU
//
//  Created by 胡波 on 2018/1/17.
//  Copyright © 2018年 胡波. All rights reserved.
//

#import "YYELessonAnswerViewController.h"
#import "UITextView+Placeholder.h"

@interface YYELessonAnswerViewController ()

@property (nonatomic,strong) UITextField *titleTF;
@property (nonatomic,strong) UITextView *textView;

@end

@implementation YYELessonAnswerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = GeneralBackColor;
    
    self.title = @"问答";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"发布" style:UIBarButtonItemStyleDone target:self action:@selector(sendClicked)];
    [self.navigationItem.rightBarButtonItem setTintColor:[UIColor blackColor]];
    
    [self configUI];
    
}

- (void)configUI {
    
    UIView *leftV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 11, 44)];
    leftV.backgroundColor = [UIColor whiteColor];
    
    self.titleTF = [[UITextField alloc]init];
    self.titleTF.placeholder = @"请输入标题";
    self.titleTF.backgroundColor = [UIColor whiteColor];
    [self.titleTF setValue:[UIFont systemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
    [self.view addSubview:self.titleTF];
    
    self.titleTF.leftView = leftV;
    self.titleTF.leftViewMode = UITextFieldViewModeAlways;
    
    UIView *contentView = [[UIView alloc]init];
    contentView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:contentView];
    
    self.textView = [[UITextView alloc]init];
    self.textView.placeholder = @"内容";
    self.textView.placeholderLabel.font = [UIFont systemFontOfSize:15];
    self.textView.font = [UIFont systemFontOfSize:15];
    self.textView.placeholderLabel.textColor = LineColor;
    [contentView addSubview:self.textView];
    

    
    if ([LoginModel shareLogin].role.integerValue == 0) {

        [self.titleTF makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view.left).offset(8);
            make.right.equalTo(self.view.right).offset(-8);
            make.top.equalTo(self.view.top).offset(11);
            make.height.equalTo(44);
        }];
        
        [contentView makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view.left).offset(8);
            make.right.equalTo(self.view.right).offset(-8);
            make.top.equalTo(self.titleTF.bottom).offset(11);
            make.height.equalTo(SCREEN_WIDTH * 0.65);
        }];
        
        [self.textView makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(UIEdgeInsetsMake(11, 11, 11, 11));
        }];
    }
    else {
        
        leftV.hidden = YES;
        [contentView makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view.left).offset(8);
            make.right.equalTo(self.view.right).offset(-8);
            make.top.equalTo(self.view.top).offset(11);
            make.height.equalTo(SCREEN_WIDTH * 0.65);
        }];
        
        [self.textView makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(UIEdgeInsetsMake(11, 11, 11, 11));
        }];
        
    }
    
}

// 发布
- (void)sendClicked {
    

    
    NSDictionary *dic = @{
                          @"uid":[LoginModel shareLogin].uid,
                          @"username":[LoginModel shareLogin].username,
                          @"token":[LoginModel shareLogin].token,
                          @"question":self.textView.text,
                          @"title":self.titleTF.text
                          };
    NSMutableDictionary *dicM = dic.mutableCopy;
    [dicM setValue:self.cid forKey:@"cid"];

    
    NSString *urlStr = nil;
    
    if ([LoginModel shareLogin].role.integerValue == 0) {
        if (self.titleTF.text.length == 0 ||
            self.textView.text.length == 0) {
            [self.view makeToast:@"请输入标题或内容"];
            return;
        }
        [dicM setValue:self.textView.text forKey:@"question"];
        [dicM setValue:self.titleTF.text forKey:@"title"];
        
        urlStr = AddQuestion;
    }
    else {
        if (self.textView.text.length == 0) {
            [self.view makeToast:@"请输入内容"];
            return;
        }
        [dicM setValue:self.textView.text forKey:@"content"];
        [dicM setValue:self.qid forKey:@"qid"];
        urlStr = Reply;

    }
    

    
    [SVProgressHUD show];
    [AFNHelper post:urlStr parameters:dicM success:^(id responseObject) {
        
        NSLog(@"%@",responseObject);
        if ([responseObject[@"status"] isEqual:@1]) {
            [self.navigationController popViewControllerAnimated:YES];
            
            if (self.block) {
                self.block();
            }
        }
        [SVProgressHUD showWithStatus:responseObject[@"message"]];
        [SVProgressHUD dismiss];
        
    } faliure:^(id error) {
        
        [SVProgressHUD showWithStatus:ShowError];
        [SVProgressHUD dismissWithDelay:0.5];
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
